<?php
	require('./include/phpsqlajax_dbinfo.php');
	
	function parseToXML($htmlStr){
		$xmlStr=str_replace('&','&amp;',$htmlStr);
		return $xmlStr; 
	}
	
	 // Opens a connection to a MySQL server.
	$connection = mysql_connect ($server, $username, $password);
	if (!$connection) 
	{
	  die('Not connected : ' . mysql_error());
	}
	
	// Sets the active MySQL database.
	$db_selected = mysql_select_db($database, $connection);
	if (!$db_selected) 
	{
	  die ('Can\'t use db : ' . mysql_error());
	}
	
	 //Set result charset to UTF-8
	 mysql_query('SET NAMES "utf8"');
	
	 // Select event categories
	 $query = 'SELECT DISTINCT eventcategory FROM eventcategories, events, events_has_eventcategories ';
	 $query .='WHERE events_has_eventcategories.EventCategories_idEventCategories = eventcategories.idEventCategories';
	 $result = mysql_query($query);
	 if (!$result) 
	 {
	  die('Invalid query: ' . mysql_error());
	 }
	
	// Creates an array of strings to hold the lines of the XML file.
	header('Content-type: text/xml');
	$xml = array('<?xml version="1.0" encoding="UTF-8"?>');
	$xml[] = '<categories>';
	
	// Iterates through the rows, printing a node for each row.
	while ($row = @mysql_fetch_assoc($result)) 
	{
	  $xml[] = '<category>';	
	  $xml[] = '<name>'.$row['eventcategory'].'</name>';
	  $xml[] = '</category>';
	} 
	
	// End XML file
	$xml[] = '</categories>';
	$xmlOutput = join($xml);
	
	//Output XML file
	echo $xmlOutput;
?>